<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.nameKeyword" placeholder="品牌名称" clearable style="width: 130px;" class="filter-item" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="品牌全称" align="center" width="290px">
        <template slot-scope="{row}">
          <router-link :to="'/manager/business/brand/'+row.id+'/edit'">
            <el-link type="primary" style="float: left;">{{ row.name }}</el-link>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="品牌简称" align="center" width="230px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.shortName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌排序" align="center" width="80px">
        <template slot-scope="{row}">
          <span style="float: right;">{{ row.brandOrder }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌说明" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.description }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="品牌添加" :visible.sync="addVisible">
      <el-form ref="addForm" :rules="addRules" :model="add" label-width="80px" style="margin-left:0px;">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="add.name" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="品牌简称" prop="shortName">
          <el-input v-model="add.shortName" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="品牌排序" prop="brandOrder">
          <el-input v-model="add.brandOrder" type="number" step="1" :min="1" :max="9999" style="width: 100px;" />
        </el-form-item>
        <el-form-item label="品牌介绍" prop="description">
          <el-input v-model="add.description" type="textarea" :rows="4" style="width: 400px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getBrandByPage, addBrand } from '@/api/manager/business-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ManagerBrand',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      beforeRouteEnterNextIgnore: true,

      listQuery: {
        pageNo: 1,
        pageSize: 20,
        nameKeyword: null
      },

      listLoading: true,
      list: null,
      total: 0,

      addVisible: false,
      add: {
        name: null,
        shortName: null,
        brandOrder: null,
        description: null
      },
      addRules: {
        name: [{
          required: true, message: '请输入品牌名称', trigger: 'blur'
        }],
        shortName: [{
          required: true, message: '请输入品牌简称', trigger: 'blur'
        }],
        brandOrder: [{
          required: true, message: '请输入品牌排序', trigger: 'blur'
        }]
      }

    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.beforeRouteEnterNextIgnore) {
        vm.getList()
      } else {
        vm.beforeRouteEnterNextIgnore = false
      }
    })
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getBrandByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        nameKeyword: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleAdd() {
      this.add = {
        name: null,
        shortName: null,
        brandOrder: null,
        description: null
      }
      this.addVisible = true
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    addConfirm() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.add))
        addBrand(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.addVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    }

  }
}

</script>
